package org.hk.kg.finance.costype.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hk.kg.db.DBContext;
import org.hk.kg.utils.Constants;

public class CostypeService implements ICostypeService{
	private static Log logger = LogFactory.getLog(CostypeService.class);
	private static final String INRT_SQL = "insert into cost_type(costTypeName,costTypeKind,active)" +
			"values('%s',%d,%d)",
			QRY_SQL = "select costTypeId,costTypeName,costTypeKind,active from cost_type",
			DEL_SQL = "delete from cost_type where costTypeId = %d",
			UPT_SQL = "update cost_type set costTypeName='${costName}',costTypeKind=${costTypeKind},active=${active} where costTypeId=${costTypeId}";
	@Override
	public boolean createCostType(String costTypeName, int costTypeKind,
			boolean isActive) {
		String sql = String.format(INRT_SQL, costTypeName,costTypeKind,isActive?1:0);
		return DBContext.instatnce.execute(Constants.DB_DEFAULT, sql) == 1;
	}
	@Override
	public List<Map> queryCostTypes() {
		return DBContext.instatnce.query(Constants.DB_DEFAULT, QRY_SQL);
	}
	@Override
	public boolean deleteCostType(int costTypeId) {
		return DBContext.instatnce.execute(Constants.DB_DEFAULT, String.format(DEL_SQL, costTypeId)) == 1;
	}
	@Override
	public boolean updateCostTypes(List<Map> params) {
		int status = 0;
		for(Map m : params)
		{
			status += DBContext.instatnce.execute(Constants.DB_DEFAULT, UPT_SQL, m);
		}
		return status == params.size();
	}

}
